import 'package:flutter/material.dart';
import 'package:flutter_basic/basic/basic_view.dart';
import 'package:flutter_basic/multi_engine/messages.dart';
import 'package:flutter_basic/multi_engine/snake_channel.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'controller.dart';

class CustomerPage extends BasicPage<CustomerPageController>{

  CustomerPage.bind(CustomerPageController controller, {String? tag}) : super.bind(controller, tag: tag);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('客户'),
        centerTitle: true,
      ),
      body: Container(
        color: Colors.white,
        child: Obx((){
          return SmartRefresher(
            enablePullDown: true,
            enablePullUp: true,
            controller: controller.refreshController,
            onRefresh: _onRefresh,
            header: WaterDropHeader(),
            onLoading: _onLoading,
            child: ListView.builder(
                itemBuilder: (context, index){
                  String info = controller.items[index];
                  return CustomerItemWidget(
                    info: info,
                    itemClick: (){
                      Map<String, Object> args = Map();
                      args['type'] = '客户详情';
                      var param = CommonParams()
                        ..arguments = args;
                      SnakeChannel().pushNativePage(param)!.then((value){
                        if(value is Map){
                          Map<String, dynamic> resultMap = Map<String, dynamic>.from(value);
                          print('关闭原生页面传递过来的值:${resultMap['name']}');
                          // controller.result.value = resultMap['name'];
                        }
                      });
                    },
                  );
                },
                itemCount: controller.items.length,
            ),
          );
        }),
      ),
    );
  }

  void _onRefresh() async{
    await Future.delayed(Duration(milliseconds: 1000));
    controller.items.value = controller.initialItems;
    controller.refreshController.refreshCompleted();
  }

  void _onLoading() async{
    await Future.delayed(Duration(milliseconds: 1000));
    controller.items.add((controller.items.length + 1).toString());
    controller.refreshController.loadComplete();
  }
}

class CustomerItemWidget extends StatelessWidget {

  final Function? itemClick;
  final String info;
  const CustomerItemWidget({required this.info, this.itemClick});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GestureDetector(
        onTap: () {
          if (itemClick != null) {
            itemClick!.call();
          }
        },
        child: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(
              color: Color(0x11000000),
              width: 1.0,
            ),
          ),
          padding: EdgeInsets.only(
            bottom: 10,
          ),
          margin: EdgeInsets.only(top: 10, bottom: 2,),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(
                    width: 100,
                    color: Color(0xffEFEFEF),
                    padding: EdgeInsets.only(left: 5, right: 5, top: 3, bottom: 5,),
                    alignment: Alignment.center,
                    child: Text(
                      '联络人(中原)',
                      style: TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                    ),
                  ),
                  Expanded(
                    child: Container(
                      child: Text(
                        'Mr.张三&$info',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: Colors.black,

                        ),
                        overflow: TextOverflow.ellipsis,
                        maxLines: 2,
                      ),
                    ).paddingOnly(
                      left: 10,
                      top: 2,
                      bottom: 2,
                      right: 10,
                    ),
                  ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(
                    width: 100,
                    alignment: Alignment.centerLeft,
                    child: Image.asset(
                      '',
                      width: 40,
                      height: 50,
                      fit: BoxFit.contain,
                    ),
                  ).marginOnly(
                    right: 10,
                    top: 10,
                    bottom: 10,
                  ),
                  Expanded(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '備註',
                            style: TextStyle(
                              fontSize: 12,
                              color: Color(0xff9B9B9B),
                            ),
                          ),
                        ),
                        Container(
                          child: Text(
                            '这是一套备注信息',
                            style: TextStyle(
                              fontSize: 12,
                              color: Color(0xff333333),
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Container(
                            padding: EdgeInsets.only(
                              left: 6,
                              right: 6,
                              top: 3,
                              bottom: 3,
                            ),
                            color: Color(0xffFFF0F0),
                            child: Text(
                              '建立時間 2021-10-25 12:30',
                              style: TextStyle(
                                fontSize: 10,
                                color: Colors.black,
                              ),
                              maxLines: 1,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ).marginOnly(left: 10, right: 10),
              Row(
                children: [
                  Container(
                    width: 100,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Text(
                            '歸屬人部門',
                            style: TextStyle(
                              fontSize: 12,
                              color: Color(0xff9B9B9B),
                            ),
                          ),
                        ),
                        Container(
                          child: Text(
                            '系统管理组',
                            style: TextStyle(
                              fontSize: 13,
                              color: Colors.black,
                            ),
                            overflow: TextOverflow.ellipsis,
                            maxLines: 2,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        child: Text(
                          '歸屬人',
                          style: TextStyle(
                            fontSize: 12,
                            color: Color(0xff9B9B9B),
                          ),
                        ),
                      ),
                      Container(
                        child: Text(
                          '小华',
                          style: TextStyle(
                            fontSize: 13,
                            color: Colors.black,
                          ),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 2,
                        ),
                      ),
                    ],
                  ).marginOnly(left: 10),
                ],
              ).marginOnly(left: 10, right: 10),
            ],
          ),
        ),
      ),
    );
  }
}
